package com.core.util;

/**
 * 用户密码加密工具类
 * 
 * @author pt-chenqw
 * 
 */
public class UserPasswordUtils {

	private final static String PREFIX = "**!";
	private final static String SUFFIX = "##";
	private final static MD5 md5 = new MD5();

	private UserPasswordUtils() {
	}
	
	/**
	 * 是否已经加密
	 * @param strPassword
	 * @return
	 */
	public static boolean isEncryptPassword(String strPassword){
		if(strPassword.startsWith(PREFIX) && strPassword.endsWith(SUFFIX) && strPassword.length() == 37){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 是否需要加密
	 * 
	 * @return
	 */
	public static boolean isNeedEncryptPassword() {
		return true;
	}
	
	/**
	 * 密码是否需要区分大小写
	 * @param flag
	 * @return
	 */
	public static boolean isUserPasswordCaseSensitive(boolean flag){
		if(!flag){
			return false;
		}else{
			return true;
		}
	}

	/**
	 * 根据用户名和密码进行加密
	 * 
	 * @param userName
	 * @param password
	 * @param flag 是否需要大小写
	 * @return 返回加密的字符串
	 */
	public static String getPassword(String userName, String password, boolean flag) {
		
		if(isEncryptPassword(password)){
			return password;
		}
		if(isUserPasswordCaseSensitive(flag)){
			password = password.toUpperCase();
		}else{
			password = password.toLowerCase();
		}
		
		if (!isNeedEncryptPassword()) {
			return password;
		}
		password = md5.getMD5ofStr(userName + "||" + password);
		return PREFIX + password + SUFFIX;
	}

}
